// 导入articleModel模块
const articleModel = require('../models/articleModel');
const Jwt = require('../common/jwt');
// 导入uuid功能（生成随机字符串）
const { v4: uuidv4 } = require('uuid');
// 编写文章类
class Article {
    // 1. 发布纯文字/图文/图片
    async addArticle(req, res) {
        let token = req.body.token || req.headers.token;
        // console.log('x,',token)
        let jwt = new Jwt(token);
        // 判断token是否在有效期
        let result = jwt.verifyToken();
        // console.log('y,',result.data)
        if (result.data) {
            req.body.article_id = uuidv4();//文章ID
            req.body.user_id = result.data;//谁写的文章
            // 执行发布文章的逻辑
            await articleModel.addArticle(req.body)
                .then(
                    content => {
                        res.send(content)
                    }
                )
                .catch(
                    err => {
                        res.send(err)
                    }
                )
        }
        else {
            res.send({
                code: -1,
                msg: '请先登录账号'
            })
        }
    }

    // 2. 获取文章
    async listArticle(req, res) {
        let page = req.query.page;
        let pageSize = req.query.pageSize;
        // console.log({page,pageSize})
        // 执行获取文章列表的逻辑
        await articleModel.listArticle(page, pageSize).then(
            content => {
                res.send(content)
            }
        )
            .catch(
                err => {
                    res.send(err)
                }
            )
    }

    // 3. 删除文章
    async removeArticle(req, res) {
        let article_id = req.body.article_id;
        // 执行删除文章的逻辑
        await articleModel.removeArticle(article_id).then(
            content => {
                res.send(content)
            }
        )
            .catch(
                err => {
                    res.send(err)
                }
            )
    }

    // 4. 获取文章(导航)
    async navArticle(req, res) {
        let page = req.query.page;
        let pageSize = req.query.pageSize;
        let nav_name = req.query.nav_name;
        // 执行获取文章列表的逻辑
        await articleModel.navArticle(nav_name, page, pageSize).then(
            content => {
                res.send(content)
            }
        )
            .catch(
                err => {
                    res.send(err)
                }
            )
    }

    // 5. 关键字搜索
    async searchArticle(req, res) {
        let keyword = req.query.keyword;
        // 执行获取文章列表的逻辑
        await articleModel.searchArticle(keyword).then(
            content => {
                res.send(content)
            }
        )
            .catch(
                err => {
                    res.send(err)
                }
            )
    }

    // 6. 获取文章(地址)
    async addressArticle(req, res) {
        let page = req.query.page;
        let pageSize = req.query.pageSize;
        let article_address = req.query.article_address;
        // 执行获取文章列表的逻辑
        await articleModel.addressArticle(article_address, page, pageSize).then(
            content => {
                res.send(content)
            }
        )
            .catch(
                err => {
                    res.send(err)
                }
            )
    }

    // 7. 获取文章详情
    async detailArticle(req, res) {
        let article_id = req.query.article_id;
        // 执行获取文章列表的逻辑
        await articleModel.detailArticle(article_id).then(
            content => {
                res.send(content)
            }
        )
            .catch(
                err => {
                    res.send(err)
                }
            )
    }
}

// 把当前类的实例挂载到模块系统下
module.exports = new Article();